﻿using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.ViewModel;
using IPR_StudyMeetingMrgSys.Common;

using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web;
using System.Web.Http;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    public class TrainCourseUserController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        [HttpGet]
        [Route("api/TrainCourseUser/GetCourseUserListByCourseIdAndDeptId")]
        public IHttpActionResult GetCourseUserListByCourseIdAndDeptId(string TCOID, string DetpId)
        {
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                string swhere = "";
                if (!string.IsNullOrWhiteSpace(TCOID)&& !string.IsNullOrWhiteSpace(DetpId))
                {
                    var str = "0";
                    if(DetpId.IndexOf(",")>-1)
                    {
                        var depids = DetpId.Split(',');
                        foreach (var item in depids)
                        {
                            str += ",'"+item+"'";
                        }
                    }
                    else
                    {
                        str = "'"+DetpId+"'";
                    }
                    swhere=" TCOID='" + TCOID + "' and DeptID in ("+str+") ";
                }
                bool blExist = false;
                int rowCount = 0;
                DataSet dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainCourseUser().GetList(swhere);
                if (dsSearch != null)
                    if (dsSearch.Tables.Count > 0)
                        if (dsSearch.Tables[0].Rows.Count > 0)
                        {
                            //返回token信息
                            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                            resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                            resultMsg.Data = dsSearch.Tables[0];
                            resultMsg.Count = rowCount;
                            blExist = true;
                        }
                if (!blExist)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = "查询结果不存在";
                    resultMsg.Data = null;
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText() + exc.Message;
                resultMsg.Data = "查询异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

    }
}
